package com.ng.application.activity.mine;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.ng.application.R;
import com.ng.application.bean.RegisterBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @Author: Ng
 * @Date: 2024/4/10
 * @Versopn: 1.0.0
 * @Description:
 */
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText register_name;
    private EditText register_nick;
    private EditText register_phone;
    private EditText register_password;
    private RadioGroup register_group;
    private RadioButton register_man;
    private RadioButton register_woman;
    private Button login_button;
    private Button register_button;

    private OkHttpClient client = new OkHttpClient();
    private Intent intent = null;

    String sex = "";
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                String result = (String) msg.obj;
                Log.i("【获取返回的信息】", result);
                final RegisterBean registerBean = new Gson().fromJson(result, RegisterBean.class);
                int resultCode = registerBean.getCode();
                if (resultCode == 200) {
                    Toast.makeText(getApplicationContext(), "注册成功！" + result, Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "注册失败！" + result, Toast.LENGTH_LONG).show();
                }
            }
        }
    };

    @Override
    protected void onCreate(@NonNull Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        register_name = (EditText) findViewById(R.id.register_name);
        register_nick = (EditText) findViewById(R.id.register_nick);
        register_phone = (EditText) findViewById(R.id.register_phone);
        register_password = (EditText) findViewById(R.id.register_password);
        register_group = (RadioGroup) findViewById(R.id.register_radio);
        register_man = (RadioButton) findViewById(R.id.register_radio_man);
        register_woman = (RadioButton) findViewById(R.id.register_radio_woman);
        login_button = (Button) findViewById(R.id.login_button);
        register_button = (Button) findViewById(R.id.register_button);

        login_button.setOnClickListener(this);
        register_button.setOnClickListener(this);

        register_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == register_man.getId()) {
                    sex = "1";
                } else {
                    sex = "0";
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_button:
                register();
                break;
            case R.id.register_button:
                intent = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void register() {
        String name = register_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "请输入用户名！", Toast.LENGTH_SHORT).show();
            return;
        }
        String nick = register_nick.getText().toString().trim();
        if (TextUtils.isEmpty(nick)) {
            Toast.makeText(this, "请输入昵称！", Toast.LENGTH_SHORT).show();
            return;
        }
        String phone = register_phone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入手机号码！", Toast.LENGTH_SHORT).show();
            return;
        }
        String password = register_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "密码不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }

        final JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("username", name);
            jsonObject.put("nickname", nick);
            jsonObject.put("phone", phone);
            jsonObject.put("password", password);
            jsonObject.put("sex", sex);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        final RequestBody requestBody = RequestBody.create(mediaType, jsonObject.toString());
        Request request = new Request.Builder()
                .post(requestBody)
                .url("http://xxxx/register")
                .build();
        okhttp3.Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("【onFailure】", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String result = response.body().string();
                Message msg = new Message();
                msg.what = 0;
                msg.obj = result;
                handler.sendMessage(msg);
            }
        });
    }
}
